<?php 
$pPageIsPublic = false;
include '../_common.php';
$_SESSION["m1"]="suggestion";
$_SESSION["m2"]="byregion";

if ($_POST['countryId']) {
	getRegions($_POST['countryId']);
	return; 
}

if (isset($_POST['regionId'])) {
	if ($_POST['action'] == 'submit'){		
	}else{
		return fetchDivingCenters(intval($_POST['regionId']));
	}
}

if ($_POST['action'] == 'submit') {
	$divcenters = $_POST['divcenter'];
	$suggest    = $_POST['suggest'];
	$regionId = $_POST['regionId'];
	
	foreach($divcenters as $key => $divcenter) {
		$tmp = new DivingCenter();
		if (! $tmp->loadByFilter('region.regionId='. $regionId 
							  .' AND '. $tmp ->getIdKey().'='. $divcenter)) {
			continue;
		} else {
			$tmp ->suggest = $suggest[$key];
			$tmp ->update('suggest');
		}
	}
	return "saved";
}

?>
<?php include("header.php"); ?>    
<script type="text/javascript">
$().ready(function(){

	$('input:checkbox.all').live('click', function() {
		$('ul#listin').find('input:checkbox').attr('checked', this.checked);
	});
	
	$('select[name="regionId"]').live('change', function() {
		return loadList(this.value);
	});

	$('input[name="btnSave"]').live('click', function() {

		var ids = "";
		var values = "";
		
		$('input:checkbox.check').each(function(){
			ids += "&divcenter[]=" + this.name;
			values += "&suggest[]=" + (this.checked ? "1" : "0");         
		});
		
		$.ajax({
			type      : "POST",
			data   	  : "action=submit&regionId=" + $('select[name="regionId"]').val() + ids + values,
			success   : function(response) {
							Notify("Suggestion for Region has been updated.", 0);	
						}
		});
	});
	
});

function loadRegion(id){
	$.ajax({
		type      : "POST",
		data   	  : "countryId=" + id,
		beforeSend: function() {
						$('span.loading').html('<img src="../images/ajax-loader.gif" />');												
					},
		success   : function(response) {
						$('span.loading').html(response);
						$('div#list').html("<h4>Select a Region</h4>");
						$('select[name="regionId"]').trigger('change');						
					}
	});
}

function loadList(id) {
	$.ajax({
		type      : "POST",
		data   	  : "regionId=" + id,
		success   : function(response) {
						$('div#list').html(response);	
					}
	});
}

</script>

<?php include("sidebar.php"); ?>
<ul id="select-nav"   >
  <li class="noborder" ><strong> Select a Destination:</strong></li>
  <li >
<?php
	$tmp = new Country();
	$tmp ->loadItems('#title ASC', null);
	Generic::gSelect($tmp, '', '', '--- select a country ---', 'width:210px;', 'onchange="return this.value ? loadRegion(this.value) : location.reload(true)"');
	unset($tmp);
?>  
  </li>
  <li class="noborder" ><strong>Region: </strong><span class="loading" > No result.</span></li>
</ul>
<div id="list">
	<h4>Select a Region</h4> 
</div>
<div class="clear"></div>    
<?php include("footer.php"); ?>

<?php 
	function getRegions($cId){
		$tmp = new Region();
		$tmp ->loadItems('#title ASC', '#countryId='. $cId);
		if (! $tmp ->rMore()) {
			echo "No result.";
		} else {
			return Generic::gSelect($tmp, '', '', '', 'width:210px;');		
		}
	}
	
	function fetchDivingCenters($regId){
		$tmp = new DivingCenter();
		$tmp ->loadItems(null, '#status="Live" AND #regionId='. $regId);
		if (! $tmp ->rMore()) {	
			echo "<h4>Zero diving center on this region.</h4>";
		} else { ?>
	<h2>Suggestions for Region</h2>
	<table id="gradient-style">
		<thead>
			<tr>
			 <th width="88%" >Diving Center</th>
			 <th width="12%" >Suggest <input type="checkbox" class="all" > </th>
			</tr>
		</thead>
	</table>
	<ul id="listin" >
<?php 		
			while ($divcenter = $tmp ->rNext()) {
?>
		<li><table id="gradient-style" >
		  <tr>
		    <td width="88%"  ><?php echo $divcenter ->title   ?></td>
		    <td width="12%" style="text-align: right; padding-right: 9px;"   >
		    <input class="check"  type="checkbox" id="divcenter[]" name="<?php echo $divcenter ->id  ?>"   <?php echo $divcenter ->suggest ? 'checked="checked"' : ''?> /></td>
		  </tr>
		</table></li>			
<?php 				
			} ?>
	</ul>
	<div style="padding-top: 8px;text-align: right; letter-spacing: 6px;" >
		<input type="button" value="Save" name="btnSave"   />
	</div>			
<?php 			
		}
	}
?>